package com.liukai.bishe.ui.activity.userInfo;

import androidx.appcompat.app.AppCompatActivity;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.liukai.bishe.R;
import com.liukai.bishe.model.doman.user.User;
import com.liukai.bishe.model.doman.user.UserMessage;
import com.liukai.bishe.presenter.userPresenter.IUpdateUserInfoPresenter;
import com.liukai.bishe.presenter.userPresenter.impl.IUpdateUserInfoPresenterImpl;
import com.liukai.bishe.utils.Constants;
import com.liukai.bishe.view.IUpdateUserInfoCallBack;

public class ChangeNickNameActivity extends AppCompatActivity implements View.OnClickListener,IUpdateUserInfoCallBack {

    private Unbinder mBind;

    @BindView(R.id.btnBack)
    public ImageView btnBack;

    @BindView(R.id.title)
    public TextView tvTitle;

    @BindView(R.id.content)
    public EditText tvContent;

    @BindView(R.id.btnSave)
    public TextView btnSave;

    @BindView(R.id.btnDelete)
    public ImageView btnDelete;
    private IUpdateUserInfoPresenter mIUpdateUserInfoPresenter;
    private Intent mIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_nick_name);

        mBind = ButterKnife.bind(this);
        initData();
        initListener();


    }

    private void initListener() {
        btnBack.setOnClickListener(this);
        tvTitle.setOnClickListener(this);
        tvContent.setOnClickListener(this);
        btnSave.setOnClickListener(this);
        btnDelete.setOnClickListener(this);
    }

    private void initData() {

        mIntent = getIntent();
        String title = mIntent.getStringExtra("title");
        String content = mIntent.getStringExtra("content");
        mIUpdateUserInfoPresenter = new IUpdateUserInfoPresenterImpl();
        mIUpdateUserInfoPresenter.registerCallback(this);
        tvTitle.setText("修改" + title);
        tvContent.setText(content);

    }


    @Override
    public void onClick(View view) {

        switch (view.getId()){

            case R.id.btnBack : // 返回
                finish();
                break;

            case R.id.title : // 标题

                break;

            case R.id.content : // 内容

                break;

            case R.id.btnDelete : // 删除
                tvContent.setText("");
                break;

            case R.id.btnSave : // 保存
                String nickName = tvContent.getText().toString();
                User user = Constants.getUserInfo();
                user.setNickname(nickName);
                mIUpdateUserInfoPresenter.update(user);
                break;

        }

    }

    @Override
    public void result(Object object) {
        UserMessage userMessage = (UserMessage)object; // 获取服务器的返回信息
        if (userMessage.getCode().equals("3001")){ // 修改成功
            mIntent.putExtra("userMessage",userMessage);
            setResult(RESULT_OK,mIntent);
//            User user = userMessage.getUser(); // 获取修改后的信息
//            Constants.setUser(user); //修改全局User
//            setResult(RESULT_OK,mIntent);
        }
        finish();
    }
}
